Repository navigation
Conversation
The name in `case <pattern> as z` was only bound by value and class patterns, via a pending `as_pattern` that the subpattern visitor had to pick up. Sequence and mapping patterns never bound it, so `z` raised UnboundLocalError. Value patterns bound the pattern value instead of the subject, so `case 1.0 as z` gave `z == 1.0` for subject `1` or `True`. Bind the name in visit_as_pattern itself, to the subject, in the block reached when the subpattern matched. This also removes the or-pattern workaround.
Collaborator
|
Thank you! I think you have issues with: |
When the subject of a match statement is a local variable, the visitor uses that variable's register directly. A capture pattern or a walrus in a guard that rebinds the variable therefore changes what the rest of the match sees, e.g. `case [x, y]` reads `x[1]` from the new `x`, and `case list([x]) as z` binds `z` to the new `x`.
When the subject was a local variable, the match visitor used the variable's register directly, so a capture pattern or a walrus in a guard that rebinds the variable changed what the rest of the match saw. Collect the variables assigned in the patterns and guards, and copy the subject to a new register if it's one of them, like for loops do for their iterable.
It's now also used for the subject of match statements, so give it a name and docstring that aren't specific to for loops.
andriisem
force-pushed
the
mypyc-match-as-capture
branch
from
November 30, 2026 13:25
af6f98b to
967fcda
Compare
Author
|
@hauntsaninja rebind fixed. the visitor used the subject variable's register directly so any pattern or guard that rebinds it broke the rest of the match (also case [x, y] and walrus in guard were broken on master). now the subject gets copied when a pattern or guard assigns to it, reusing the helper from the for loop fix (#22137). added tests floatint I think it's out of scope PR, wdyt? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
compiled
aspatterns in match statements bind the name wrongcase [1, *_] as z: z is the item 1, not the subjectcase 1.0 as z: z is the literal, sof(1)returns 1.0only value and class patterns bound the name, and the value pattern used the literal. now
visit_as_patternbinds the subject after the subpattern matches. this removesbind_as_patternand the workaround invisit_or_pattern